/**
 * @author: Gene
 * @age: 永远18岁的美少年
 * @Email： yangjianyun@58.com
 * @date: 2021-12-01 21:06:05
 * @description:
 */

// 声明一个函数type
type GetName = (firstName: string, lastName: string) => string;

let getName: GetName = function (firstName: string, lastName: string): string {
    return firstName + lastName
}

// 函数可选参数
function print(name: string, age?: number): void {
    console.log(name, age);
}

type Method = "GET" | "POST" | "PUT" | "DELETE"

function ajax(url: string, method: Method = "GET") {
    console.log(url, method);
}

function hello(name: string): void {
    console.log("hello ->", name);
}

hello("Gene")

/** =================== 函数的重载 =============== */
/**
 * 两个,或两个以上的同名函数, 提供多个函数声明, reudx 的 compose.ts 应用了重载
 */
let obj: any = {}

/**
 * 如果 val 值是字符串,  传递给 obj.name
 * 如果 val 值是数字, 传递给 obj.age
 * @param val
 */
function attr(val: string): void
function attr(val: number): void

function attr(val: any): void {
    if (typeof val === "string") {
        obj.name = val
    } else {
        obj.age = val
    }
}

attr("Gene")